//
//  RootScene.m
//  RuntimeDemo
//
//  Created by 李明明 on 15/8/10.
//  Copyright (c) 2015年 LMM. All rights reserved.
//

#import "RootScene.h"
#import "LTableView.h"
@interface RootScene ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, NSFileManagerDelegate>
@property (weak, nonatomic) IBOutlet LTableView *tableView;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, strong) UILabel *label;
@end

@implementation RootScene
{
    NSFileManager *defaulter;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    self.height = 40;
    self.tableView.bounds = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height);
    defaulter = [NSFileManager defaultManager];
    defaulter.delegate = self;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSLog(@"%f", cell.frame.size.height);
    cell.frame = CGRectMake(0, indexPath.row * 44, 0, 0);
    [UIView animateWithDuration:0.1 animations:^{
        cell.frame = CGRectMake(0, indexPath.row * 44, self.view.frame.size.width, 44);
    } completion:^(BOOL finished) {
        cell.frame = CGRectMake(0, indexPath.row * 44, self.view.frame.size.width, 44);
    }];

    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rootcellreuse"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rootcellreuse"];
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%zd", indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 20;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *indexStr = [NSString stringWithFormat:@"%zd", indexPath.row];
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [arr firstObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:indexStr];
    [defaulter fileExistsAtPath:filePath];
    // 创建文件
//    [defaulter createFileAtPath:filePath contents:nil attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30]}];
//    NSError *error = nil;
    // 创建目录
//   BOOL fileCreate = [defaulter createDirectoryAtPath:indexStr withIntermediateDirectories:NO attributes:nil error:&error];
//    BOOL result = [defaulter removeItemAtPath:indexStr error:&error];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return self.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.01;
}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    self.label = [[UILabel alloc] init];
//    self.label.text = @"向下拉加载";
//    self.label.textAlignment = NSTextAlignmentCenter;
//    return self.label;
//}
//
//// 结束拖拽
//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    if ([self.label.text isEqualToString:@"松手开始加载"]) {
//        [UIView animateWithDuration:0.5 animations:^{
//            
//            self.tableView.bounds = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
//        } completion:^(BOOL finished) {
//            [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
//                // 在这写网络请求
//                self.tableView.bounds = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height);
//            } completion:^(BOOL finished) {
//                
//            }];
//        }];
//    }else if ([self.label.text isEqualToString:@"向下拉加载"] && self.tableView.contentOffset.y < 40){
//        [UIView animateWithDuration:0 animations:^{
//            
//        } completion:^(BOOL finished) {
//            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
//                self.tableView.bounds = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height);
//            } completion:^(BOOL finished) {
//                
//            }];
//        }];
//    }else {
//        
//    }
//}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    if ([self.label.text isEqualToString:@"松手开始加载"]) {
//        [UIView animateWithDuration:0.5 animations:^{
//            
//            self.tableView.bounds = CGRectMake(0, 0, self.tableView.frame.size.width, self.tableView.frame.size.height);
//        } completion:^(BOOL finished) {
//            [UIView animateWithDuration:0.5 delay:1 options:UIViewAnimationOptionAllowUserInteraction animations:^{
//                // 在这写网络请求
//                self.tableView.bounds = CGRectMake(0, 40, self.tableView.frame.size.width, self.tableView.frame.size.height);
//            } completion:^(BOOL finished) {
//                
//            }];
//        }];
//    }
//    
//}
//// 正在滑动
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (self.tableView.contentOffset.y <= 0) {
//        self.label.text = @"松手开始加载";
//    }else {
//        self.label.text = @"向下拉加载";
//    }
//}
//
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
